package com.ruoyi.project.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysEarnings;
import com.ruoyi.project.system.domain.SysQueryPay;
import com.ruoyi.project.system.mapper.SysEarningsMapper;
import com.ruoyi.project.system.mapper.SysQueryPayMapper;
import com.ruoyi.project.system.service.ISysEarningsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Random;

/**
 * 收益Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@Service
public class SysEarningsServiceImpl implements ISysEarningsService {
    @Autowired
    private SysEarningsMapper sysEarningsMapper;

    @Autowired
    private SysQueryPayMapper sysQueryPayMapper;

    @Autowired
    private RedisCache redisCache;

    // 100%中奖 50%中奖 20%中奖
    private static int[][] CJHS = {{5, 4, 3, 2, 1}, {0, 5, 0, 4, 0, 3, 0, 2, 0, 1}, {0, 0, 0, 0, 1, 0, 0, 0, 0, 4, 0, 0, 0, 0, 3, 0, 0, 0, 0, 2, 0, 0, 0, 0, 5}};

    private static float[] DJED = {0F, 0.2F, 0.4F, 0.6F, 0.8f};

    @Override
    @Transactional
    public AjaxResult shakeRedPackage(SysQueryPay sysQueryPay) {
        // 算可摇金额
        float ZDHB = Float.parseFloat(redisCache.getCacheObject("ZDHB").toString());
        float ZXHB = Float.parseFloat(redisCache.getCacheObject("ZXHB").toString());

        // 获取使用的抽奖盒
        String CJHBH = redisCache.getCacheObject("CJHBH").toString();
        int[] CJH = CJHS[Integer.parseInt(CJHBH)];
        // 摇余额
        int randomNumber = new Random().nextInt(CJH.length);
        int dramIdx = CJH[randomNumber];
        float ydhb = ZXHB + DJED[dramIdx - 1] * (ZDHB - ZXHB);
        // 减次数 改余额
        SysQueryPay sqp;
        BigDecimal redPacket = sysQueryPay.getRedPacket() == null ? new BigDecimal(0) : sysQueryPay.getRedPacket();
        if (dramIdx == 0) {    // 没有摇到
            sqp = new SysQueryPay(sysQueryPay.getOrderNo(), sysQueryPay.getRedPacketNum() - 1, redPacket);
        } else {
            sqp = new SysQueryPay(sysQueryPay.getOrderNo(), sysQueryPay.getRedPacketNum() - 1, redPacket.add(BigDecimal.valueOf(ydhb)));
            // 增加红包收益
            sysEarningsMapper.insertSysEarnings(new SysEarnings(
                    SecurityUtils.getUserId(),
                    "red_packet",
                    sysQueryPay.getOrderNo(),
                    BigDecimal.valueOf(ydhb)));
        }
        sysQueryPayMapper.updateSysQueryPay(sqp);
        AjaxResult result = new AjaxResult();
        result.put("dramIdx", dramIdx);
        result.put("ydhb", ydhb);
        return result;
    }

    /**
     * 查询收益
     *
     * @param earningsId 收益主键
     * @return 收益
     */
    @Override
    public SysEarnings selectSysEarningsByEarningsId(Long earningsId) {
        return sysEarningsMapper.selectSysEarningsByEarningsId(earningsId);
    }

    /**
     * 查询收益列表
     *
     * @param sysEarnings 收益
     * @return 收益
     */
    @Override
    public List<SysEarnings> selectSysEarningsList(SysEarnings sysEarnings) {
        return sysEarningsMapper.selectSysEarningsList(sysEarnings);
    }

    /**
     * 新增收益
     *
     * @param sysEarnings 收益
     * @return 结果
     */
    @Override
    public int insertSysEarnings(SysEarnings sysEarnings) {
        sysEarnings.setCreateTime(DateUtils.getNowDate());
        return sysEarningsMapper.insertSysEarnings(sysEarnings);
    }

    /**
     * 修改收益
     *
     * @param sysEarnings 收益
     * @return 结果
     */
    @Override
    public int updateSysEarnings(SysEarnings sysEarnings) {
        return sysEarningsMapper.updateSysEarnings(sysEarnings);
    }

    /**
     * 批量删除收益
     *
     * @param earningsIds 需要删除的收益主键
     * @return 结果
     */
    @Override
    public int deleteSysEarningsByEarningsIds(Long[] earningsIds) {
        return sysEarningsMapper.deleteSysEarningsByEarningsIds(earningsIds);
    }

}
